% This is the second try by wwb on June 1st

% Basic Variables Assumption
Line = xlsread('Simulated.xlsx');  % As an example, the Line is a "1*50" row matrix.
ci = 40 ; cI = 200; r = 100; I = 8;
p = [0.1,0.5,0.9];
p1 = p(1); p2 = p(2); p3 = p(3);
ti = 30;
lambda = 3;

% Step1: Initialization
S = zeros(8,3);
Profit = zeros(50,8);   
MarginalP = zeros(1,51);
i_best = zeros(1,51);
% For each one, the marginal profit to put into slot i.

% Step2: Start looping
for n = 1:50      % Wait for the nth call
    j = Line(n);    % The nth call is of type j
    for i = 1:8     % Find the fitted slot of patient n
        rim = zeros(8,3);
        rim(i,j) = 1;         % By "rim", I mean "interim", that's temporary.
        rimS = S + rim;   % Important step. 
        % After above, the "rimS" is the status now.
        % Find the dimention of Q and R.
        Nmax = max(sum(rimS'));   % Use transpose of matrix.
        % Now, calculate Matrix Q(i,:) with Function 7
        Q = findQ(rimS, p, Nmax);
        % Now calculate the value of R(i,:) with function 9 and 10
        R = findR(rimS, p, Nmax, Q);
        % R needs to be improved in the future.
        
        % Use the profit function!
        Profit(n,i) = profit(Q,R,r,ci);     
    end
    
    [Value, i_b] = max(Profit(n,:));    % The i_b is the best i for nth person.
    TotalP(1,n) = Value;
    i_best(n) = i_b;
    
    % Above part should get the best choice of i (by Comparison).
    delta = zeros(8,3);      % Have to repete for every patient (n).
    delta(i_b,j) = 1;              % Here, j is directly read, i is calculated.
    S = S + delta;              % It have to be here.
end

TotalP